package com.idc.resource.manager;

import com.idc.resource.dal.dto.TBasicCityCode;
import com.idc.resource.dal.mapper.TBasicCityCodeMapper;
import com.idc.resource.enums.ErrorCodeEnum;
import com.idc.resource.exception.IdcServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 城市信息管理
 * Created by Lijing on 2017/7/13.
 */
@Slf4j
@Component
public class CityInfoManager {

    /** 城市信息数据库操作 */
    @Autowired
    private TBasicCityCodeMapper cityCodeMapper;

    /**
     * 根据省份名称模糊查询省份信息
     * @param provinceName 省份名称
     * @return 省份信息
     */
    public List<TBasicCityCode> queryProvinces(String provinceName){
        if(StringUtils.isBlank(provinceName)){
            provinceName=null;
        }else {
            provinceName="%"+provinceName+"%";
        }
        try {
            return cityCodeMapper.selectProvinces(provinceName);
        }catch (Exception e){
            log.error("根据省份名称:{},模糊查询省份信息失败，原因:",provinceName,e);
            throw new IdcServiceException(ErrorCodeEnum.DB_ERROR.getCode(),ErrorCodeEnum.DB_ERROR.getDesc());
        }
    }

    /**
     * 根据省份编号查询城市信息
     * @param provinceCode 省份编号
     * @return 城市信息
     */
    public List<TBasicCityCode> queryCitys(String provinceCode){
        try {
            return cityCodeMapper.selectCity(provinceCode);
        }catch (Exception e){
            log.error("根据省份编号:{},查询城市信息失败，原因:",provinceCode,e);
            throw new IdcServiceException(ErrorCodeEnum.DB_ERROR.getCode(),ErrorCodeEnum.DB_ERROR.getDesc());
        }
    }

    /**
     * 获取地市信息
     * @param provinceCode 省份编码
     * @param cityCode 城市编码
     * @return 地市信息
     */
    public List<TBasicCityCode> queryCounties(String provinceCode,String cityCode){
        try {
            return cityCodeMapper.selectCounty(provinceCode, cityCode);
        }catch (Exception e){
            log.error("根据省份编码:{}，城市编码:{},查询地市信息失败，原因:",provinceCode, cityCode,e);
            throw new IdcServiceException(ErrorCodeEnum.DB_ERROR.getCode(),ErrorCodeEnum.DB_ERROR.getDesc());
        }
    }

}
